import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class checkScheduleFrame extends JDialog{
	
	// GUI
	private JPanel roomPanel;
	private JLabel roomLabel;
	private MyComboBoxModel model;
	private JComboBox roomComboBox;
	private JPanel textPanel;
	private JScrollPane scrollpane;
	private JTextArea recordTextArea;
	private JPanel okPanel;
	private JButton okButton;
	
	// handlers
	private RoomHandler roomHandler;
	private OkHandler okHandler;
	// data control
	private roomManager rm;
	private int option;
	
	public checkScheduleFrame(roomManager initRm){
		rm = initRm;
		option = 0;
		layoutGUI();
	}
	
	public void layoutGUI(){
		
		this.setTitle("Schedule Check");
		roomLabel = new JLabel("Room: ");
		model = new MyComboBoxModel();
		Vector<String> roomNums = new Vector<String>();
		roomNums.add("");
		for (int i=1; i<=16; i++)
			roomNums.add(i+"");
		model.setList(roomNums);
		roomComboBox = new JComboBox(model);
		roomPanel = new JPanel();
		roomPanel.add(roomLabel);
		roomPanel.add(roomComboBox);
		
		recordTextArea = new JTextArea(30,100);
		recordTextArea.setEditable(false);
		scrollpane = new JScrollPane(recordTextArea);
		textPanel = new JPanel();
		textPanel.add(scrollpane);
		
		okButton = new JButton("OK");
		okPanel = new JPanel();
		okPanel.add(okButton);
		
		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);
		this.add(roomPanel, layout.NORTH);
		this.add(textPanel, layout.CENTER);
		this.add(okPanel, layout.SOUTH);
		
		// add handlers
		okHandler = new OkHandler();
		roomHandler = new RoomHandler();
		okButton.addActionListener(okHandler);
		roomComboBox.addActionListener(roomHandler);

		// size and locate the dialog at the center of the screen
		this.pack();
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}
	
	// for test
	public static void main(String[] args){
		roomManager rm = new roomManager();
		checkScheduleFrame temp = new checkScheduleFrame(rm);
		temp.setVisible(true);
	}
	
	class RoomHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			recordTextArea.setText("");
			try{
				int roomNum = Integer.parseInt((String) ((JComboBox) arg0.getSource()).getModel().getSelectedItem());
				String s = rm.reservationString(roomNum);
				recordTextArea.append(s);
			}
			catch(NumberFormatException nfe){}
		}
		
	}
	
	class OkHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			checkScheduleFrame.this.setVisible(false);
		}
		
	}
}
